
class_name CTaskInfoArray
extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal MaskID_changed(old_value, new_value) 
signal TaskInfoID_changed(old_value, new_value) 
signal StartValue_changed(old_value, new_value) 
signal TaskID_changed(old_value, new_value) 
signal TaskState_changed(old_value, new_value) 
signal Progress_changed(old_value, new_value) 
signal createTime_changed(old_value, new_value) 
signal IsShow_changed(old_value, new_value) 
signal ReceiveNumber_changed(old_value, new_value) 



class STaskInfo:
    var nIndex
    var tblOwner
    var m_tblAlias
    var bInited = false
    func _init(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.TaskInfoID = 0	# 任务id
        self.StartValue = 0	# 任务开始进度值
        self.TaskID = 0	# 任务id
        self.TaskState = 0	# 任务状态
        self.Progress = 0	# 任务进度
        self.createTime = 0	# 创建时间
        self.IsShow = 0	# 是否需要显示在客户端列表
        self.ReceiveNumber = 0	# 每天可接取该任务的次数

        self.nIndex = -1
        self.tblOwner = owner
        self.m_tblAlias = {
 
        }

    func GetIndex():
        return self.nIndex

    func SetZeroValue(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.TaskInfoID = 0	# 任务id
        self.StartValue = 0	# 任务开始进度值
        self.TaskID = 0	# 任务id
        self.TaskState = 0	# 任务状态
        self.Progress = 0	# 任务进度
        self.createTime = 0	# 创建时间
        self.IsShow = 0	# 是否需要显示在客户端列表
        self.ReceiveNumber = 0	# 每天可接取该任务的次数


    func UnPack(_bufferStream, _bOffset):
        if _bOffset :
            _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)

        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.TaskInfoID = _bufferStream.ReadInt64()
        self.StartValue = _bufferStream.ReadInt64()
        self.TaskID = _bufferStream.ReadInt64()
        self.TaskState = _bufferStream.ReadUint8()
        self.Progress = _bufferStream.ReadUint32()
        self.createTime = _bufferStream.ReadUint32()
        self.IsShow = _bufferStream.ReadUint16()
        self.ReceiveNumber = _bufferStream.ReadUint32()

        self.bInited = true
        if self.tblOwner :
            self.tblOwner.DispatchEvent('uuid', [self, 0, self.nIndex])

    func CopyFrom(other):
        self.nIndex = other.nIndex
        self.TaskInfoID = other.TaskInfoID
        self.StartValue = other.StartValue
        self.TaskID = other.TaskID
        self.TaskState = other.TaskState
        self.Progress = other.Progress
        self.createTime = other.createTime
        self.IsShow = other.IsShow
        self.ReceiveNumber = other.ReceiveNumber


    func Update(_bufferStream, _nIndex ):
        _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)
        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.TaskInfoID = _bufferStream.ReadInt64()
        self.StartValue = _bufferStream.ReadInt64()
        self.TaskID = _bufferStream.ReadInt64()
        self.TaskState = _bufferStream.ReadUint8()
        self.Progress = _bufferStream.ReadUint32()
        self.createTime = _bufferStream.ReadUint32()
        self.IsShow = _bufferStream.ReadUint16()
        self.ReceiveNumber = _bufferStream.ReadUint32()


    func OnDataChanged( fieldName, oldValue, newValue ):
        if oldValue == newValue or not self.tblOwner:
            return
        self.tblOwner.emit_signal(fieldName, oldValue, newValue)


    #  STaskInfo Geter Setter method # 
    var UUID:
        set(_value):
            var old = self.m_UUID
            self.UUID = _value
            if self.tblOwner:
                self.tblOwner.UUID_changed(old, _value)
        get:
            return UUID

    var MaskID:
        set(_value):
            var old = self.m_MaskID
            self.MaskID = _value
            if self.tblOwner:
                self.tblOwner.MaskID_changed(old, _value)
        get:
            return MaskID

    var TaskInfoID:
        set(_value):
            var old = self.m_TaskInfoID
            self.TaskInfoID = _value
            if self.tblOwner:
                self.tblOwner.TaskInfoID_changed(old, _value)
        get:
            return TaskInfoID

    var StartValue:
        set(_value):
            var old = self.m_StartValue
            self.StartValue = _value
            if self.tblOwner:
                self.tblOwner.StartValue_changed(old, _value)
        get:
            return StartValue

    var TaskID:
        set(_value):
            var old = self.m_TaskID
            self.TaskID = _value
            if self.tblOwner:
                self.tblOwner.TaskID_changed(old, _value)
        get:
            return TaskID

    var TaskState:
        set(_value):
            var old = self.m_TaskState
            self.TaskState = _value
            if self.tblOwner:
                self.tblOwner.TaskState_changed(old, _value)
        get:
            return TaskState

    var Progress:
        set(_value):
            var old = self.m_Progress
            self.Progress = _value
            if self.tblOwner:
                self.tblOwner.Progress_changed(old, _value)
        get:
            return Progress

    var createTime:
        set(_value):
            var old = self.m_createTime
            self.createTime = _value
            if self.tblOwner:
                self.tblOwner.createTime_changed(old, _value)
        get:
            return createTime

    var IsShow:
        set(_value):
            var old = self.m_IsShow
            self.IsShow = _value
            if self.tblOwner:
                self.tblOwner.IsShow_changed(old, _value)
        get:
            return IsShow

    var ReceiveNumber:
        set(_value):
            var old = self.m_ReceiveNumber
            self.ReceiveNumber = _value
            if self.tblOwner:
                self.tblOwner.ReceiveNumber_changed(old, _value)
        get:
            return ReceiveNumber



# ================================= array ==========================
var m_UUID
var m_nTaskInfoCount
var m_TaskInfo

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nTaskInfoCount = 0
    self.m_TaskInfo = []
    var nMaxIndex = self.GetTaskInfoMaxCount()-1
    self.m_TaskInfo.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_TaskInfo[i] = STaskInfo.new(self) 
        self.m_TaskInfo[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nTaskInfoCount = _bufferStream.ReadUint32()
    for i in range(self.m_nTaskInfoCount):
        var elem = STaskInfo.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_TaskInfo[i] = elem

func GetTableName():
    return 'TaskInfo'

func GetUUID():
    return self.m_UUID

func GetTaskInfoCurNum():
    return self.m_nTaskInfoCount

func SetTaskInfoCurNum( nValue ):
    self.m_nTaskInfoCount = nValue

func SetCurNum(nValue):
    self.m_nTaskInfoCount = nValue

static func GetTaskInfoMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO

func GetTaskInfoMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO

func GetTaskInfo(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO:
        print("GetTaskInfoEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO")
        return null

    return self.m_TaskInfo[_nIndex];

func SetTaskInfo(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO:
        print("SetTaskInfoEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO")
        return null
    
    var old = self.m_TaskInfo[_nIndex]
    var elem = STaskInfo.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_TaskInfo[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddTaskInfo(_Prop, _nIndex):
    var nCurNum = self.m_nTaskInfoCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO:
        print("AddTaskInfoEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO");
        return null;

    var elem = STaskInfo.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_TaskInfo[i] = self.m_TaskInfo[i - 1]
            self.m_TaskInfo[i].nIndex = i
    self.m_TaskInfo[_nIndex] = elem
    self.m_nTaskInfoCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelTaskInfo(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nTaskInfoCount:
        return
    
    self.m_nTaskInfoCount = self.m_nTaskInfoCount - 1
    if _nIndex != self.m_nTaskInfoCount:
        self.m_TaskInfo[_nIndex] = self.m_TaskInfo[self.m_nTaskInfoCount]

    # var nLastDataID = self.m_TaskInfo[self.m_nTaskInfoCount]
    var elem = self.m_TaskInfo[_nIndex]
    for i in range(_nIndex + 1, self.m_nTaskInfoCount - 1):
        self.m_TaskInfo[i - 1] = self.m_TaskInfo[i]
        self.m_TaskInfo[i - 1].nIndex = i - 1
    self.m_TaskInfo[self.m_nTaskInfoCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nTaskInfoCount = _pOther.m_nTaskInfoCount
    for i in range(_pOther.m_nTaskInfoCount):
        self.m_TaskInfo[i] = STaskInfo.new(self)
        self.m_TaskInfo[i].CopyFrom(_pOther.m_TaskInfo[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nTaskInfoCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO:
        # var szLog = string.format("CTaskInfoArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxTaskInfoEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_TASKINFO)
        return false
    var elem = null
    var old = null
    if self.m_TaskInfo[_fieldId] != null:
        elem = self.m_TaskInfo[_fieldId]
        old = STaskInfo.new( self )
        old.CopyFrom( elem )
    else:
        elem = STaskInfo.new(self)
        self.m_TaskInfo[_fieldId] = elem
        self.m_TaskInfo[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nTaskInfoCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = STaskInfo.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_TaskInfo[_fieldId]:
        self.m_TaskInfo[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nTaskInfoCount, _fieldId+1, -1):
            self.m_TaskInfo[index] = self.m_TaskInfo[index- 1]
            self.m_TaskInfo[index].m_UUID = uuid + index - _fieldId
            self.m_TaskInfo[index].nIndex = index
        self.m_TaskInfo[_fieldId] = elem;
        self.m_TaskInfo[_fieldId].nIndex = _fieldId

    self.m_nTaskInfoCount = self.m_nTaskInfoCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_TaskInfo[_fieldId] == null:
        return 
    
    self.m_nTaskInfoCount = self.m_nTaskInfoCount - 1
    var elem = self.m_TaskInfo[_fieldId]
    if _fieldId != self.m_nTaskInfoCount:
        var uuid = self.m_TaskInfo[_fieldId].m_UUID;
        self.m_TaskInfo[_fieldId] = self.m_TaskInfo[self.m_nTaskInfoCount]
        self.m_TaskInfo[_fieldId].m_UUID = uuid
        self.m_TaskInfo[_fieldId].nIndex = _fieldId

    self.m_TaskInfo[self.m_nTaskInfoCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nTaskInfoCount-1):
        if funFilter.call(self.m_TaskInfo[i], i):
            tblItems.append(self.m_TaskInfo[i])
    return tblItems

func QueryByTaskID(TaskID):
    var nCnt = self.m_nTaskInfoCount
    for i in range(nCnt-1):
        var curElem = self.m_TaskInfo[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_TaskID == TaskID :
                return curElem
    return null


